package com.xlh.service.courseTest.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ZipUtil;
import com.xlh.dto.courseTest.PaperReadingDTO;
import com.xlh.exception.common.GlobalException;
import com.xlh.properties.ResourcesProperties;
import com.xlh.service.courseTest.CourseTestPaperTemplateService;
import com.xlh.util.WkhtmltopdfUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

@Slf4j
@Service("courseTestPaperTemplateService")
public class CourseTestPaperTemplateServiceImpl implements CourseTestPaperTemplateService {

    @Autowired
    private ResourcesProperties resourcesProperties;

    private ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    private TemplateEngine templateEngine = new TemplateEngine();

    final String TEMPLATE_PREFIX = "static/tempfiles/";
    final String TEMPLATE_SUFFIX = ".html";
    {
        resolver.setPrefix(TEMPLATE_PREFIX);
        resolver.setSuffix(TEMPLATE_SUFFIX);
        templateEngine.setTemplateResolver(resolver);
    }

    @Override
    public String createPaperInfoZip(List<PaperReadingDTO> paperReadingDTOS) {

        String floder = RandomUtil.randomStringUpper(10);
        String pdfPath = resourcesProperties.getPaper().getLocalDir()+ File.separator + "pdf" + File.separator + floder;
        String htmlPath = resourcesProperties.getPaper().getLocalDir()+ File.separator +"html" + File.separator + floder;
        String mappingUrl = resourcesProperties.getPaper().getMappingUrl()+ File.separator + "pdf" ;


        for (int i = 0; i < paperReadingDTOS.size(); i++) {

            PaperReadingDTO paperReadingDTO = paperReadingDTOS.get(i);
            String fileNo = FileUtil.cleanInvalid(paperReadingDTO.getUserNumber() + "_" + paperReadingDTO.getUserName());
            String htmlFile = htmlPath + File.separator + fileNo +".html";
            FileUtil.touch(htmlFile);
            String pdfFile = pdfPath + File.separator + fileNo +".pdf";
            FileUtil.touch(pdfFile);

            try {
                //填充数据
                Context context = new Context();
                context.setVariable("paperReadingDTO", paperReadingDTO);
                //渲染模板生成静态
                FileWriter writer = new FileWriter(htmlFile);
                templateEngine.process("course_test", context, writer);
            } catch (Exception e) {
               log.error("转换失败：" + e);
            }
            if (!WkhtmltopdfUtil.html2pdf(htmlFile, pdfFile)){
                throw new GlobalException("转换失败");
            }
        }

        File zip = ZipUtil.zip(pdfPath);
        return mappingUrl + File.separator + zip.getName();
    }
}